package com.ruoyi.framework.config;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.Constants;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.CacheControl;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        // 移除默认的消息转换器，这样可以接受任意内容类型
        converters.clear();
        // 添加自定义的消息转换器，如果需要的话
        // converters.add(...);
    }


    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException, ServletException {
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        HttpServletRequest request= (HttpServletRequest) servletRequest;

        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "GET,POST,DELETE,PUT,OPTIONS");
        response.setHeader("Access-Control-Allow-Headers", "*");
        response.setHeader("Access-Control-Allow-Credentials", "false");
        response.setIntHeader("Access-Control-Max-Age", 3600);
        filterChain.doFilter(servletRequest, servletResponse);
    }

    /*自动刷新上传的文件*/
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry)
    {
        System.out.println("文件路径");
//        System.out.println(RuoYiConfig.getProfile()+"/");
//        System.out.println(Constants.RESOURCE_PREFIX+"/**");

        /** 本地文件上传路径 */
//        registry.addResourceHandler(Constants.RESOURCE_PREFIX + "/**")
//                .addResourceLocations("file:" + RuoYiConfig.getProfile() + "/");

//        String jarPath = this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
//        System.out.println("Jar包路径：" + jarPath);

//        System.out.println(System.getProperty("user.dir"));
//
//        String rootPath = getClass().getResource("/").getPath().split("target")[0];
//        System.out.println("项目根路径：" + rootPath);
//
//        //System.getProperty("user.dir") = D:\客户网站\广东盈舜项目管理有限公司 投标项目\yingshunCode代码\RuoYi
//        //String jvmStartPath = System.getProperty("user.dir")+"\\ruoyi-admin\\src\\main\\resources\\static\\images\\attachments\\";
//        String jvmStartPath = rootPath.replace("file:/","")+"src/main/resources/static/images/attachments/";
//        System.out.println("JVM启动目录：" + jvmStartPath);
        String jvmStartPath ="C:\\attachments\\";
        System.out.println("JVM启动目录：" + jvmStartPath);


        //String path="D:\\客户网站\\广东盈舜项目管理有限公司 投标项目\\yingshunCode代码\\RuoYi\\ruoyi-admin\\src\\main\\resources\\static\\images\\attachments\\";
        registry.addResourceHandler("/attachments/**").addResourceLocations("file:"+jvmStartPath);


    }

    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        // 不配置任何特定的内容协商规则，允许任何内容类型
    }
}
